﻿using Customer.Read.API.Dtos;
using Customer.Domain;
using Customer.Read.API.Applcation.Command.SalesCommand;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Customer.Domain.Dto;
using Customer.Read.API.Applcation.CommandHandlers.SalesRefundCommandHandler;
using Customer.Read.API.Applcation.Command.SalesRefundCommand;
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
using System.IdentityModel.Tokens.Jwt;
using Customer.Read.API.Applcation.Command.RefundCommand;

namespace Customer.Read.API.Controllers
{
    /// <summary>
    /// 销售管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class SalesController : ControllerBase
    {
        IMediator mediator;

        public SalesController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 反填销售
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<SalesProductDto>> FanSales([FromQuery]SalesFanCommand command)
        {
            ApiResult<SalesProductDto> result = new ApiResult<SalesProductDto>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 显示销售
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Page<SalesProductDto>>> GetSalesList([FromQuery] SalesSelCommand command)
        {
            ApiResult<Page<SalesProductDto>> result = new ApiResult<Page<SalesProductDto>>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                result.Data = await mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 显示销售详情
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<SalesProductDto>>> GetSalesDetail([FromQuery] SalesDetailCommand command)
        {
            ApiResult<List<SalesProductDto>> result = new ApiResult<List<SalesProductDto>>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                result.Data = await mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }



        /// <summary>
        /// 显示销售退货 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Page<SalesRefundDto>>> GetSalesRefund([FromQuery] SalesRefundShowCommand command)
        {
            ApiResult<Page<SalesRefundDto>> result = new ApiResult<Page<SalesRefundDto>>();
            try
            {
                result.Code = 200;
                result.Message = "显示成功";
                result.Data = await mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 显示销售退货 查看详情
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<SalesRefundDto>> GetSalesRefundView([FromQuery] SalesRefundViewCommand command)
        {
            ApiResult<SalesRefundDto> result = new ApiResult<SalesRefundDto>();
            try
            {
                result.Code = 200;
                result.Message = "显示成功";
                result.Data = await mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 显示操作列的退货
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<SalesRefundDto>>> GetRefundDetail([FromQuery] SalesRefundListCommand command)
        {
            ApiResult<List<SalesRefundDto>> result = new ApiResult<List<SalesRefundDto>>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                result.Data = await mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }



    }
}
